package com.common.report.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;

import com.common.doc.xsl.POIFManager;
import com.common.report.vo.ReportTableViewVO;

public class ReportServlet extends HttpServlet {
	protected Logger log = Logger.getLogger(this.getClass());

	protected HttpServletRequest req;

	protected HttpServletResponse res;

	protected JFreeChart chart;
	protected int chartWidth=400;
	public void expXls() {
		OutputStream output = null;
		try {
			output = res.getOutputStream();
			byte b[] = new byte[1024];
			ReportTableViewVO viewVO = (ReportTableViewVO) req.getSession()
					.getAttribute("viewVO");
			POIFManager _poif = POIFManager.createCell4T();
//			_poif.iteratorValue4XLS(viewVO);
			String docName = viewVO.getReportName() + ".xls";
			docName = java.net.URLEncoder.encode(docName, "UTF-8");
			res.setHeader("Content-Disposition", "attachment;filename="
					+ new String(docName.getBytes("UTF-8"), "gb2312"));
			res.setContentType("application/vnd.ms-excel;charset=gb2312");
			_poif.writ2OutStream(output);
		} catch (Exception e) {
			log.error(e.getMessage());
		} finally {
			try {
				output.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

	}

	public void expChart() {
		log.info("==========expChart============");
		res.setContentType("application/x-msdownload");
		String reportName = (String) req.getSession()
				.getAttribute("reportName");
		String docName = reportName + ".png";
		try {
			docName = java.net.URLEncoder.encode(docName, "UTF-8");

			res.setHeader("Content-Disposition", "attachment;filename="
					+ new String(docName.getBytes("UTF-8"), "GBK"));
			chart = (JFreeChart) req.getSession().getAttribute("chart");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			ChartUtilities.writeChartAsJPEG(res.getOutputStream(), chart, chartWidth,
					400);
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}

	}
}
